package org.fatmansoft.teach.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import org.fatmansoft.teach.security.jwt.AuthEntryPointJwt;
import org.fatmansoft.teach.security.jwt.AuthTokenFilter;
import org.fatmansoft.teach.security.services.UserDetailsServiceImpl;

@Configuration  //内部包含@Bean注解的方法时必须使用！
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        // securedEnabled = true,
        // jsr250Enabled = true,
        prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Autowired
    private AuthEntryPointJwt unauthorizedHandler;

    @Bean
    //@Bean注解用于告诉方法，产生一个Bean对象，然后这个Bean对象交给Spring管理随后这个Spring将会将这个Bean对象放在自己的IOC容器中；
    //@Bean是一个方法级别上的注解，主要用在@Configuration注解的类里，也可以用在@Component注解的类里。添加的bean的id为方法名!
    public AuthTokenFilter authenticationJwtTokenFilter() {
        return new AuthTokenFilter();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {  //注入AuthenticationManager
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() { //注入加密模式
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();
        http.cors().and().csrf().disable()  //关闭csrf功能
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() //不通过session获取SecurityManager
                .authorizeRequests()
                .antMatchers("/api/auth/login").permitAll()  //允许登陆接口的匿名访问
//                .antMatchers("/api/auth/**").permitAll()
//                .antMatchers("/api/html/**").permitAll()
//                .antMatchers("/public/**").permitAll()
//                .antMatchers("/login.html").permitAll()
//                .antMatchers("/mainFrame.html").permitAll()
//                .antMatchers("/pages/**").permitAll()
//                .antMatchers("/basicInfo/**").permitAll()
                .anyRequest().authenticated();  //       其他端口均需要授权

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
    }
}
